using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CPlatePrintSys.Domain.Models;
using CPlatePrintSys.Domain.Services;
using CPlatePrintSys.Infrastructure.Repositories;
using Newtonsoft.Json;

namespace CPlatePrintSys.Application.Services
{
    /// <summary>
    /// 全局配置服务实现
    /// </summary>
    public class GlobalConfigService : IGlobalConfigService
    {
        private readonly IGlobalConfigRepository _configRepository;

        public GlobalConfigService(IGlobalConfigRepository configRepository)
        {
            _configRepository = configRepository;
        }

        public async Task<RoundingRuleConfig> GetRoundingRuleConfigAsync()
        {
            var configValue = await _configRepository.GetValueByKeyAsync(GlobalConfigKeys.RoundingRule);
            if (string.IsNullOrEmpty(configValue))
            {
                // 返回默认配置
                return new RoundingRuleConfig();
            }

            try
            {
                return JsonConvert.DeserializeObject<RoundingRuleConfig>(configValue);
            }
            catch (JsonException)
            {
                // 如果JSON解析失败，返回默认配置
                return new RoundingRuleConfig();
            }
        }

        public async Task UpdateRoundingRuleConfigAsync(RoundingRuleConfig config, string updatedBy)
        {
            var configValue = JsonConvert.SerializeObject(config, Formatting.Indented);
            await _configRepository.UpdateValueAsync(GlobalConfigKeys.RoundingRule, configValue, updatedBy);
        }

        public async Task<LayerColorConfig> GetLayerColorConfigAsync()
        {
            var configValue = await _configRepository.GetValueByKeyAsync(GlobalConfigKeys.LayerColor);
            if (string.IsNullOrEmpty(configValue))
            {
                return new LayerColorConfig();
            }

            try
            {
                return JsonConvert.DeserializeObject<LayerColorConfig>(configValue);
            }
            catch (JsonException)
            {
                return new LayerColorConfig();
            }
        }

        public async Task UpdateLayerColorConfigAsync(LayerColorConfig config, string updatedBy)
        {
            var configValue = JsonConvert.SerializeObject(config, Formatting.Indented);
            await _configRepository.UpdateValueAsync(GlobalConfigKeys.LayerColor, configValue, updatedBy);
        }

        public async Task<OutputDirectoryConfig> GetOutputDirectoryConfigAsync()
        {
            var configValue = await _configRepository.GetValueByKeyAsync(GlobalConfigKeys.OutputDirectory);
            if (string.IsNullOrEmpty(configValue))
            {
                return new OutputDirectoryConfig();
            }

            try
            {
                return JsonConvert.DeserializeObject<OutputDirectoryConfig>(configValue);
            }
            catch (JsonException)
            {
                return new OutputDirectoryConfig();
            }
        }

        public async Task UpdateOutputDirectoryConfigAsync(OutputDirectoryConfig config, string updatedBy)
        {
            var configValue = JsonConvert.SerializeObject(config, Formatting.Indented);
            await _configRepository.UpdateValueAsync(GlobalConfigKeys.OutputDirectory, configValue, updatedBy);
        }

        public async Task<InterfaceConfig> GetInterfaceConfigAsync()
        {
            var configValue = await _configRepository.GetValueByKeyAsync(GlobalConfigKeys.InterfaceConfig);
            if (string.IsNullOrEmpty(configValue))
            {
                return new InterfaceConfig();
            }

            try
            {
                return JsonConvert.DeserializeObject<InterfaceConfig>(configValue);
            }
            catch (JsonException)
            {
                return new InterfaceConfig();
            }
        }

        public async Task UpdateInterfaceConfigAsync(InterfaceConfig config, string updatedBy)
        {
            var configValue = JsonConvert.SerializeObject(config, Formatting.Indented);
            await _configRepository.UpdateValueAsync(GlobalConfigKeys.InterfaceConfig, configValue, updatedBy);
        }

        public async Task<string> GetConfigValueAsync(string configKey)
        {
            return await _configRepository.GetValueByKeyAsync(configKey);
        }

        public async Task UpdateConfigValueAsync(string configKey, string configValue, string updatedBy)
        {
            await _configRepository.UpdateValueAsync(configKey, configValue, updatedBy);
        }

        public async Task<List<GlobalConfig>> GetAllConfigsAsync()
        {
            var configs = await _configRepository.GetAllAsync();
            return new List<GlobalConfig>(configs);
        }

        public async Task<bool> ConfigExistsAsync(string configKey)
        {
            return await _configRepository.ExistsAsync(configKey);
        }

        public async Task<string> GetRoundingRuleForStageAsync(string stage)
        {
            var config = await GetRoundingRuleConfigAsync();
            return config.GetRoundingRule(stage);
        }

        public async Task<string> GetOutputDirectoryPathAsync()
        {
            var config = await GetOutputDirectoryConfigAsync();
            return config.OutputPath;
        }

        public async Task<string> GetCadWinSharePathAsync()
        {
            var config = await GetInterfaceConfigAsync();
            return config.CadWinSharePath;
        }

        public async Task<string> GetKmacsApiUrlAsync()
        {
            var config = await GetInterfaceConfigAsync();
            return config.KmacsApiUrl;
        }
    }
} 